package com.itheima.controller;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 32338
 * @date 2024-05-02 15:22:45
 * @description 检查项控制层
 */
@RestController
@RequestMapping("/checkitem")
@Slf4j
public class CheckItemController {

    @Autowired
    private CheckItemService checkItemService;


    /**
     * @description
     ***:新增检查项
     **/
    @PostMapping("/add")
    public Result addCheckItem(@RequestBody CheckItem checkItem){
        log.info("新增检查项:{}",checkItem);

        checkItemService.save(checkItem);

        return new Result(true,MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    /**
     * @description
     ***:删除检查项
     **/
    @GetMapping
    public Result delete(Integer id){
        log.info("删除成功：{}",id);

        checkItemService.delete(id);

        return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    /**
     * @description
     ***:根据检查组id查询关联的检查项id
     **/
    @GetMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer CheckGroupId){
        List<Integer> list=checkItemService.findCheckItemIdsByCheckGroupId(CheckGroupId);
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
    }

    /**
     * @description
     ***:修改回显检查项
     **/
    @GetMapping("/findById")
    public Result findById(Integer id){
        log.info("根据id查询回显数据：{}",id);

        CheckItem checkItem = checkItemService.findById(id);

        return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItem);
    }

    /**
     * @description
     ***:修改检查项
     **/
    @PostMapping("/edit")
    public Result updateCheckItem(@RequestBody CheckItem checkItem){
        log.info("修改检查项：{}",checkItem);

        checkItemService.updateCheckItem(checkItem);

        return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    /**
     * @description
     ***:分页查询
     **/
    @PostMapping("/findPage")
    public PageResult page(@RequestBody QueryPageBean queryPageBean){
        log.info("根据项目编码/项目名称分页查询检查项:{}",queryPageBean);

        PageResult pageResult = checkItemService.page(queryPageBean);

        return pageResult;
    }

    /**
     * @description
     ***:查询所有检查项
     **/
    @GetMapping("/findAll")
    public Result findAll(){
       List<CheckItem> checkItemList = checkItemService.findAll();
        return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItemList);
    }
}
